package com.app.twth.ui.finalprocess;

import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.app.twth.app.Constant;
import com.app.twth.app.MyApplication;
import com.app.twth.base.BaseAndroidViewModel;
import com.app.twth.bean.BaseResponseEntity;
import com.app.twth.bean.LoadingBean;
import com.app.twth.bean.PrintSku;
import com.app.twth.net.JsonCallBack;
import com.app.twth.ui.checkin.FileUpload;
import com.app.twth.utils.DateUtil;
import com.app.twth.utils.Utils;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;


/**
 * ****************************************************************
 * 文件名称: LoginViewModel
 * 文件描述:
 * 注意事项:
 * 版权声明:
 * ****************************************************************
 */
public class FinalProcessViewModel extends BaseAndroidViewModel<FinalProcessDataModel> {
    public MutableLiveData<FinalProcessBatchList> batchListLiveData = new MutableLiveData<>();
    public MutableLiveData<List<FinalProcessDetail>> batchDetailLiveData = new MutableLiveData<>();
    public MutableLiveData<FinalProcess> finalProcessLiveData = new MutableLiveData<>();
    public MutableLiveData<FinalProcess.ItemInfoDTO> trackingLiveData = new MutableLiveData<>();
    public MutableLiveData<FileUpload> fileUploadMutableLiveData = new MutableLiveData<>();

    public FinalProcessViewModel(@NonNull Application application) {
        super(application);
    }

    public void returnFinalProcessBatchList() {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.returnFinalProcessBatchList(new JsonCallBack<BaseResponseEntity<FinalProcessBatchList>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<FinalProcessBatchList>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<FinalProcessBatchList> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    batchListLiveData.setValue(baseResponseEntity.data);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void returnFinalProcessCheckBatchNo(String batchNo) {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.returnFinalProcessCheckBatchNo(batchNo, new JsonCallBack<BaseResponseEntity<FinalProcess>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<FinalProcess>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<FinalProcess> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    finalProcessLiveData.setValue(baseResponseEntity.data);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void returnFinalProcessDetail(String batchNo) {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.returnFinalProcessDetail(batchNo, new JsonCallBack<BaseResponseEntity<List<FinalProcessDetail>>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<List<FinalProcessDetail>>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<List<FinalProcessDetail>> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    batchDetailLiveData.setValue(baseResponseEntity.data);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void returnFinalProcessItem(String batchNo, String ibLpn) {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.returnFinalProcessItem(batchNo, ibLpn, new JsonCallBack<BaseResponseEntity<ArrayList<FinalProcess.ItemInfoDTO>>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<ArrayList<FinalProcess.ItemInfoDTO>>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<ArrayList<FinalProcess.ItemInfoDTO>> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    FinalProcess finalProcess = new FinalProcess();
                    finalProcess.setIbLpnItemList(baseResponseEntity.data);
                    FinalProcess.ItemInfoDTO itemInfoDTO = new FinalProcess.ItemInfoDTO();
                    itemInfoDTO.setBatchNo(batchNo);
                    itemInfoDTO.setIbLpn(ibLpn);
                    finalProcess.setItemInfo(itemInfoDTO);
                    finalProcessLiveData.setValue(finalProcess);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void returnFinalProcessInfo(int id) {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.returnFinalProcessInfo(id, new JsonCallBack<BaseResponseEntity<FinalProcess.ItemInfoDTO>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<FinalProcess.ItemInfoDTO>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<FinalProcess.ItemInfoDTO> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    trackingLiveData.setValue(baseResponseEntity.data);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void returnFinalProcessPrintLpn(FinalProcessQr finalProcessQr) {
        finalProcessQr.setAccountId(null);
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.returnFinalProcessPrintLpn(finalProcessQr, new JsonCallBack<BaseResponseEntity<FinalProcessPrintLpn>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<FinalProcessPrintLpn>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<FinalProcessPrintLpn> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    FinalProcessPrintLpn data = baseResponseEntity.data;
                    if (data == null) {
                        return;
                    }
                    if (Utils.isEmpty(data.getData())) {
                        return;
                    }
                    MyApplication.printerService.printPDFBase64(baseResponseEntity.data.getData());
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void printSku(PrintSku printSku) {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.printSku(printSku, new JsonCallBack<BaseResponseEntity<FinalProcessPrintLpn>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<FinalProcessPrintLpn>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<FinalProcessPrintLpn> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    FinalProcessPrintLpn data = baseResponseEntity.data;
                    if (data == null) {
                        return;
                    }
                    if (Utils.isEmpty(data.getData())) {
                        return;
                    }
                    MyApplication.printerService.printPDFBase64(baseResponseEntity.data.getData());
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void returnFinalProcessSubmit(FinalProcessQr finalProcessQr) {
        finalProcessQr.setAccountId(null);
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.returnFinalProcessSubmit(finalProcessQr, new JsonCallBack<BaseResponseEntity<FinalProcess>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<FinalProcess>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<FinalProcess> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    finalProcessLiveData.setValue(baseResponseEntity.data);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void fileUpload(String containerNo, String fileName, String path, int type, int position, boolean isSubmit, boolean showDialog) {
        isLoading.setValue(new LoadingBean(showDialog, Constant.loading));
        errorResultCode.setValue("");
        String name = "preprocess/" + containerNo + "-" + fileName + "-" + DateUtil.getYMD() + "-" + System.currentTimeMillis() + ".jpg";
        mModel.upload(name, path, new JsonCallBack<BaseResponseEntity<String>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<String>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<String> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    FileUpload fileUpload = new FileUpload();
                    fileUpload.setType(type);
                    fileUpload.setPosition(position);
                    fileUpload.setPath(baseResponseEntity.data);
                    fileUpload.setSubmit(isSubmit);
                    Log.e("TAG", "type():" + type + ",position:" + position);
                    fileUploadMutableLiveData.setValue(fileUpload);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
                errorResultCode.setValue(baseResponseEntity.getCode());
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(error);
            }
        });
    }

}
